package com.ice_hufs.medical_in_myhand;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.UnsupportedEncodingException;

import com.loopj.android.http.*;


/**
 *
 *
 * Created by heeyeong on 2014-11-04.
 */
public class User_join extends Activity {
    private EditText email_ET;
    private EditText password_ET;
    private EditText pwd_confirm_ET;
    private EditText name_ET;
    private EditText address_ET;

    TextView password_check;

    String email;
    String password;
    String password_confirm;
    String name;
    String gender;
    String address;

    int able_submit = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
                , WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_join_user);


        email_ET = (EditText) findViewById(R.id.email_input);
        password_ET = (EditText) findViewById(R.id.password_input);
        pwd_confirm_ET = (EditText) findViewById(R.id.pwd_confirm_input);
        name_ET = (EditText)findViewById(R.id.name_input);
        address_ET = (EditText)findViewById(R.id.address_input);

        password_check = (TextView)findViewById(R.id.pwd_check_view);

        Button female_radioBtn = (Button)findViewById(R.id.female_radioBtn);
        female_radioBtn.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                gender = "female";
            }
        });


        Button male_radioBtn = (Button)findViewById(R.id.male_radioBtn);
        male_radioBtn.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                gender = "male";
            }
        });

        Button joinBtn = (Button)findViewById(R.id.joinBtn);
        joinBtn.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                email = email_ET.getText().toString();
                password = password_ET.getText().toString();
                password_confirm = pwd_confirm_ET.getText().toString();

                if(password.compareTo(password_confirm) != 0){
                    password_check.setText("Password input does not match. Try again.");
                    able_submit = 1;
                }else if (password.compareTo(password_confirm) == 0){
                    password_check.setText("Password input does match.");
                    able_submit = 2;
                }

                name = name_ET.getText().toString();
                address = address_ET.getText().toString();


                if(able_submit == 2)
                    submit();

            }

        });

    }
    private void submit()
    {
        RequestParams params = new RequestParams();
        params.put("email", email);
        params.put("password", password);
        params.put("name", name);
        params.put("gender", gender);
        params.put("address", address);


        String url = "http://54.68.142.141:5000/user/regist";

        AsyncHttpClient client = new AsyncHttpClient();
        client.post(url, params, new AsyncHttpResponseHandler()
        {
            @Override
            public void onSuccess(int arg0, org.apache.http.Header[] arg1,
                                  byte[] arg2) {
                // TODO Auto-generated method stub
                try
                {
                    String content = new String(arg2, "UTF-8");
                    Log.d("login test", content);

                    if (arg0 == 200)
                    {
                        Toast toast = Toast.makeText(getApplicationContext(),"Please login", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();

                        Intent my_Intent= new Intent(User_join.this, login.class);
                        startActivity(my_Intent);
                        finish();

                    }
                }
                catch (UnsupportedEncodingException e)
                {
                    Toast.makeText(getBaseContext(), "Error occurred.", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int arg0, org.apache.http.Header[] arg1,
                                  byte[] arg2, Throwable arg3) {
                // TODO Auto-generated method stub
                Log.d("login test", Integer.toString(arg0));
                if(arg0 != 200){
                    Toast toast = Toast.makeText(getApplicationContext(),"fail to join", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                }

            }

        });
    }
}
